package com.ali.pogo;

import com.ali.sqlSession.SqlSession;

import javax.sql.DataSource;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class Configuration {
    private DataSource source;
    Map<String,MappedStatement> mappedStatementMap = new HashMap<>();
    private MapperRegistry mapperRegistry;

    public Configuration() {
        this.mapperRegistry = new MapperRegistry(this);
    }

    public DataSource getSource() {
        return source;
    }

    public void setSource(DataSource source) {
        this.source = source;
    }

    public Map<String, MappedStatement> getMappedStatementMap() {
        return mappedStatementMap;
    }

    public void setMappedStatementMap(Map<String, MappedStatement> mappedStatementMap) {
        this.mappedStatementMap = mappedStatementMap;
    }

    /**
     * 判断是否存在对应的statementID
     * @param statementId
     * @return
     */
    public boolean hasStatementId(String statementId){
        return this.mappedStatementMap.containsKey(statementId);
    }

    public <T> T getMapper(Class<T> type,SqlSession sqlSession) throws Exception {
        return this.mapperRegistry.getMapper(type,sqlSession);
    }

    public boolean hasMapper(Class<?> type){
        return this.mapperRegistry.hasMapper(type);
    }

    public void addMapper(Class<?> aClass) {
        try {
            this.mapperRegistry.addMapper(aClass);
        }catch (Exception e){

        }
    }
}
